package com.deer.wms.review.manage.web;

import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.BeanUtils;
import com.deer.wms.review.manage.model.*;
import com.deer.wms.review.manage.service.CaseDetailService;
import com.deer.wms.review.manage.service.CaseMasterService;
import com.deer.wms.review.manage.service.CaseUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import springfox.documentation.annotations.ApiIgnore;


/**
* Created by guo on 2018/10/24.
*/
@Api(description = "审核内容详细信息api接口")
@RestController
@RequestMapping("/case/details")
public class CaseDetailController {

    @Autowired
    private CaseDetailService caseDetailService;

    @Autowired
    private CaseMasterService caseMasterService;

    @Autowired
    private CaseUserService caseUserService;

    @PostMapping("/add")
    public Result add(@RequestBody CaseDetail caseDetail) {
        caseDetailService.save(caseDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/delete")
    public Result delete( Integer caseDetailId) {
        caseDetailService.removeById(caseDetailId);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody CaseDetail caseDetail) {
        caseDetailService.updateById(caseDetail);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        CaseDetail caseDetail = caseDetailService.getById(id);
        return ResultGenerator.genSuccessResult(caseDetail);
    }



    @GetMapping("/list")
    public Result list(CaseDetailCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<CaseDetailDto> list = caseDetailService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }


    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/nowList")
    public Result nowList(CaseDetailCriteria criteria, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null ){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        criteria.setUserId(currentUser.getUserId());
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<CaseDetailDto> list = caseDetailService.findNowList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }



    @ApiImplicitParams({
            @ApiImplicitParam(name = "access-token", value = "token", paramType = "header", dataType = "String", dataTypeClass = String.class,  required = true)
    })
    @GetMapping("/nowDetails")
    public Result nowDetails(String billNo, @ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null ){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }

        /***
         * 查询caseMaster
         */
        CaseMaster caseMaster = caseMasterService.findByBillNo(billNo);
        if(caseMaster ==null){
            throw new ServiceException(CommonCode.CASE_NULL);
        }

        /**
         * 查询caseDetail
         */

        List<CaseDetailDto>list = caseDetailService.findByMasterId(caseMaster.getCaseMasterId());

        /**
         * 遍历details,
         * 查询caseUsers
         * 赋值
         */
//        for(int i=0;i<list.size();i++){
//
//            Integer caseDetailId = list.get(i).getCaseDetailId();
//            List<CaseUser> caseUsers = caseUserService.findByCaseDetailId(caseDetailId);
//            for(int y=0;y<caseUsers.size();y++){
//                CaseUser caseUser = new CaseUser();
//                caseUser.setCaseDetailId(caseUser.getCaseDetailId());
//                caseUser.setUserId(caseUser.getUserId());
//                caseUser.setUserName(caseUser.getUserName());
//                caseUser.setReviewResult(caseUser.getReviewResult());
//                caseUser.setMemo(caseUser.getMemo());
//            }
//
//        }

        return ResultGenerator.genSuccessResult(list);
    }

}
